!define APPNAME EsiTux-winmenu
!ifndef DISTRO
    !define DISTRO esitux
!endif
!ifndef APPREVISION
    !define APPREVISION 0
!endif
!define VERSION "1"
!define APPFULLNAME "${APPNAME}-v${VERSION}"
!define OutFile "${APPFULLNAME}.exe"
!define WUBI_COMMAND "\EsiTux-wininstaller.exe" #absolute path without drive
!define INSTALL_COMMAND "\EsiTux-wininstaller.exe --install --32bit" #absolute path without drive
!define CDBOOT_COMMAND "\EsiTux-wininstaller.exe --cdboot"
!define VERTICAL_IMAGE "data\images\${DISTRO}-vertical.bmp" #(recommended size: 164x314 pixels).
!define ICON_FILE "data\images\${DISTRO}.ico"

!include MUI2.nsh
!include LogicLib.nsh
!include StrFunc.nsh
!include FileFunc.nsh
!include TextFunc.nsh

!insertmacro LineRead
${StrTok}
${StrStr}

Name $distro
BrandingText " "
Caption $(window_caption)
OutFile ${OutFile}
XPStyle on
ShowInstDetails nevershow

#VARS
Var drive
Var dir
Var distro
Var debug
Var cdinfo
Var version
Var dialog
Var link1
Var action
Var VERTICAL_IMAGE_file
Var VERTICAL_IMAGE
Var cd_button
Var cd_label
Var wubi_button
Var wubi_label
Var info_button
Var info_label
Var cdboot_checkbox
Var website

#Pages
!define MUI_ICON ${ICON_FILE}
!define MUI_WELCOMEFINISHPAGE_BITMAP ${VERTICAL_IMAGE}
!define MUI_CUSTOMFUNCTION_GUIINIT .onGUIStart
!define MUI_PAGE_CUSTOMFUNCTION_PRE pre_welcome
!define MUI_PAGE_CUSTOMFUNCTION_SHOW show_welcome
!insertmacro MUI_PAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_PRE pre_finish
!define MUI_PAGE_CUSTOMFUNCTION_SHOW show_finish
!define MUI_FINISHPAGE_TITLE $(cd_reboot_title)
!define MUI_FINISHPAGE_TEXT_REBOOT $(cd_reboot_text)
!insertmacro MUI_PAGE_FINISH

#l10n
!include translator.nsh

#Macros
!insertmacro GetDrives

!macro DebugAlert msg
    ${if} $debug != ""
        messagebox MB_OK "${msg}"
    ${endif}
!macroend
!define DebugAlert "!InsertMacro DebugAlert"

!macro AddControlBlock name left top
    #Button
    nsDialogs::CreateControl /NOUNLOAD BUTTON ${DEFAULT_STYLES}|${WS_TABSTOP}|${BS_FLAT} 0 ${left} ${top}u 120u 18u "$(${name}_button)"
    Pop $${name}_button
    ${NSD_OnClick} $${name}_button action_${name}
    #SetCtlColors $${name}_label "" "${MUI_BGCOLOR}"
    #Label
    !ifdef ${name}_top
        !undef ${name}_top
    !endif
    !define /math ${name}_top ${top} + 20
    nsDialogs::CreateControl /NOUNLOAD STATIC ${DEFAULT_STYLES}|${SS_LEFT} ${WS_EX_TRANSPARENT} ${left} ${${name}_top}u 200u 38u "$(${name}_label)"
    Pop $${name}_label
    SetCtlColors $${name}_label "" "${MUI_BGCOLOR}"
!macroend
!define AddControlBlock "!InsertMacro AddControlBlock"

#Functions
Function pre_welcome
    call change_icon
    call change_vertical_image
FunctionEnd

Function show_welcome
    LockWindow on
    ShowWindow $mui.WelcomePage.Title ${SW_HIDE}
    ShowWindow $mui.WelcomePage.Text ${SW_HIDE}
    ShowWindow $mui.Button.Next ${SW_HIDE}
    ShowWindow $mui.Button.Back ${SW_HIDE}
    ${AddControlBlock} cd 120u 20
  #  ${if} ${FileExists} "$EXEDIR${WUBI_COMMAND}"
#	${DebugAlert} "$EXEDIR${WUBI_COMMAND}"
 #   ${orif} $debug != ""
#	${DebugAlert} "$EXEDIR${WUBI_COMMAND}"
        ${AddControlBlock} wubi 120u 80
        ${AddControlBlock} info 120u 140
 #   ${else}
        ${AddControlBlock} info 120u 80
  #  ${endif}
    ${NSD_SetFocus} $cd_button
    LockWindow off
FunctionEnd

Function pre_finish
    ${if} $action != "cd"
        abort
    ${endif}
    SetRebootFlag true
FunctionEnd

Function show_finish
    LockWindow on
    ShowWindow $mui.Button.Next ${SW_NORMAL}
    ShowWindow $mui.Button.Back ${SW_NORMAL}
    ${NSD_CreateRadioButton} 120u 135u 140u 20u $(cdboot_checkbox)
    Pop $cdboot_checkbox
    SetCtlColors $cdboot_checkbox "" "${MUI_BGCOLOR}"
    ${NSD_OnClick} $mui.FinishPage.RebootNow action_reboot
    ${NSD_OnClick} $mui.FinishPage.RebootLater action_reboot_later
    ${NSD_OnClick} $cdboot_checkbox action_cdboot
    SendMessage $mui.FinishPage.RebootNow ${BM_SETCHECK} ${BST_CHECKED} 0
    LockWindow off
FunctionEnd

Function action_cd
    strcpy $action cd
    SendMessage $HWNDPARENT "0x408" "1" ""
FunctionEnd

Function action_wubi
    strcpy $action wubi
    SendMessage $HWNDPARENT "0x408" "1" ""
FunctionEnd

Function action_info
    strcpy $action info
    ${DebugAlert} "Website http://$website"
    ExecShell "open" "http://$website"
FunctionEnd

Function action_cdboot
    strcpy $action cdboot
FunctionEnd

Function action_reboot
    strcpy $action cd
FunctionEnd

Function action_reboot_later
    strcpy $action cd
FunctionEnd

Function detect_distro
    strcpy $distro ${DISTRO}
    #Test exedir first
    strcpy $9 $EXEDIR 2
    #call test_cd
    ${if} "$distro" != ""
        ${DebugAlert} "Found CD $drive, distro=$distro"
        return
    ${endif}
    ${GetDrives} "CDROM+HDD" test_cd
    ${if} "$distro" != ""
        ${DebugAlert} "Found CD $drive, distro=$distro"
    ${else}
        ${if} $debug != ""
            ${DebugAlert} "$(invalid_CD)($distro), but ignoring because we are in debug mode."
			#strcpy $distro "esitux"			
        ${else}
            messagebox MB_OK|MB_ICONSTOP "$(invalid_CD)"
            quit
        ${endif}
    ${endif}
FunctionEnd

Function test_cd
    strcpy $drive $9 2
    ${DebugAlert} "     testing drive $drive"
    ${if} ${FileExists} "$drive\casper\filesystem.squashfs"
    ${andif} ${FileExists} "$drive\.disk\info"
        ClearErrors
        ${LineRead} "$drive\.disk\info" "1" $cdinfo
        ${if} ${errors}
            strcpy $cdinfo ""
            ClearErrors
            ${DebugAlert} $cdinfo
        ${endif}
    ${else}
        ${DebugAlert} "No $drive\.disk\info"
    ${endif}
    ${if} "$cdinfo" == ""
    ${andif} $debug != ""
        strcpy $cdinfo "$debug"
    ${endif} 
    ${if} "$cdinfo" != ""
        ${DebugAlert} "     cdinfo=$cdinfo"
        ${StrTok} $distro "$cdinfo"   " " "0" "1"
        ${StrTok} $version "$cdinfo" " " "1" "1"
        ${DebugAlert} "     distro=$distro, version=$version"
        ${if} "$version" == "${VERSION}"
            push "StopGetDrives"
            return
        ${endif}
    ${endif}
    strcpy $distro ""
    strcpy $version ""
    push "GetAnotherDrive"
FunctionEnd

#Function change_header
# Disabled for time being
#    ${if} ${FileExists} "$PLUGINSDIR\data\images\$distro-header.bmp"
#        SetBrandingImage /IMGID=1046 /RESIZETOFIT "$PLUGINSDIR\data\images\$distro-header.bmp"
#    ${endif}
#    ClearErrors
#FunctionEnd

Function change_vertical_image
    #Must be called within the page
    ${if} ${FileExists} "$PLUGINSDIR\data\images\$distro-vertical.bmp"
        #Exploits some nsis weirdness, such as hardcoded filenames, works with nsis 2.34
        CopyFiles /SILENT "$PLUGINSDIR\data\images\$distro-vertical.bmp" "$PLUGINSDIR\modern-wizard.bmp"
    ${else}
        ${DebugAlert} "Could not find vertical image: $PLUGINSDIR\data\images\$distro-vertical.bmp"
    ${endif}
    ClearErrors
FunctionEnd

Function change_icon
    ${if} ${FileExists} "$PLUGINSDIR\data\images\$distro.ico"
        System::Call 'user32::LoadImage(i 0, t "$PLUGINSDIR\data\images\$distro.ico", i ${IMAGE_ICON}, i 0, i 0, i ${LR_LOADFROMFILE}) i.s'
        pop $0
        SendMessage $HWNDPARENT ${WM_SETICON} 0 $0
        System::Call gdi32::DeleteObject(i$0)
    ${else}
        ${DebugAlert} "Could not find icon: $PLUGINSDIR\data\images\$distro.ico"
    ${endif}
    ClearErrors
FunctionEnd

Function parse_command_line
    ${StrStr} $debug $CMDLINE "--debug"
    ${if} $debug != ""
        ${StrTok} $debug "$debug" "=" "1" "1"
        ${if} $debug == ""
            strcpy $debug "Mythbuntu 8.10 (fake cdinfo for debugging purposes)"
        ${endif}
    ${endif}
FunctionEnd

Function .onInit
    InitPluginsDir
    SetOutPath $PLUGINSDIR
    file /r "data"
    call parse_command_line
    call detect_distro
    ReadINIStr $website $PLUGINSDIR\data\websites.ini "websites" "$distro"
    call change_icon
FunctionEnd

Function .onInstSuccess
FunctionEnd

Function .onGUIStart
FunctionEnd

Function .onGUIEnd
    ${if} $action == "wubi"
        ${DebugAlert} "Executing: $drive${WUBI_COMMAND}"
        Exec "$EXEDIR${INSTALL_COMMAND}"
    ${elseif} $action == "cdboot"
        ${DebugAlert} "Executing: $drive${CDBOOT_COMMAND}"
        Exec "$EXEDIR${CDBOOT_COMMAND} "
    ${endif}
FunctionEnd

Section Main
SectionEnd

